package com.carrentalplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrentalplatform.mapper.EvaluationMapper;
import com.carrentalplatform.model.Evaluation;
import com.carrentalplatform.service.CarRentalShopService;
import com.carrentalplatform.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 评价服务实现类
 */
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {

    @Autowired
    private CarRentalShopService carRentalShopService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addEvaluation(Evaluation evaluation) {
        // 设置创建时间
        evaluation.setCreateTime(LocalDateTime.now());
        
        // 保存评价
        boolean result = this.save(evaluation);
        
        if (result) {
            // 更新车行评分
            Double avgScore = calculateShopAvgScore(evaluation.getShopId());
            carRentalShopService.updateShopScore(evaluation.getShopId(), avgScore);
        }
        
        return result;
    }

    @Override
    public IPage<Evaluation> getUserEvaluations(Long userId, int page, int size) {
        Page<Evaluation> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Evaluation::getUserId, userId);
        queryWrapper.orderByDesc(Evaluation::getCreateTime);
        
        return this.page(pageParam, queryWrapper);
    }

    @Override
    public IPage<Evaluation> getShopEvaluations(Long shopId, int page, int size) {
        Page<Evaluation> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Evaluation::getShopId, shopId);
        queryWrapper.orderByDesc(Evaluation::getCreateTime);
        
        return this.page(pageParam, queryWrapper);
    }

    @Override
    public Evaluation getOrderEvaluation(Long orderId) {
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Evaluation::getOrderId, orderId);
        
        return this.getOne(queryWrapper);
    }

    @Override
    public Double calculateShopAvgScore(Long shopId) {
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Evaluation::getShopId, shopId);
        
        List<Evaluation> evaluations = this.list(queryWrapper);
        
        if (evaluations == null || evaluations.isEmpty()) {
            return 5.0; // 默认5分
        }
        
        // 计算平均分
        BigDecimal totalScore = BigDecimal.ZERO;
        for (Evaluation evaluation : evaluations) {
            totalScore = totalScore.add(evaluation.getScore());
        }
        
        BigDecimal avgScore = totalScore.divide(new BigDecimal(evaluations.size()), 2, RoundingMode.HALF_UP);
        
        return avgScore.doubleValue();
    }
} 